package com.jiyun.service.impl;

import com.github.wxpay.sdk.WXPayUtil;
import com.jiyun.HttpClient;
import com.jiyun.service.WxPayService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WxPayServiceImpl implements WxPayService {
    @Override
    public String getQrCodeUrl(String orers) throws Exception {
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");// https://api.mch.weixin.qq.com/pay/unifiedorder
        httpClient.setHttps(true);
        // 对接微信支付统一下单接口，封装需要的必填参数
        /*
        1. appid：微信公众账号或开放平台APP的唯一标识 wxab8acb865bb1637e
        2. mch_id：商户号  (配置文件中的partner) 11473623
        3. partnerkey：商户密钥 2ab9071b06b9f739b950ddb41db2690d
         */
        Map map = new HashMap();
        map.put("appid","wxab8acb865bb1637e");// 公众账号id
        map.put("mch_id","11473623");
        map.put("nonce_str", WXPayUtil.generateNonceStr());
        map.put("body","会员VIP充值");
        map.put("out_trade_no",orers);// 订单号
        map.put("total_fee","1");
        map.put("spbill_create_ip","127.0.0.1");
        map.put("notify_url","127.0.0.1");
        map.put("trade_type","NATIVE");
        String xmlParam = WXPayUtil.generateSignedXml(map,"2ab9071b06b9f739b950ddb41db2690d");
        httpClient.setXmlParam(xmlParam);
        httpClient.post();
        String result = httpClient.getContent();
        Map<String,String> resultMap = WXPayUtil.xmlToMap(result);
        String codeUrl = resultMap.get("code_url");
        System.out.println("获取到的二维码链接地址为："+codeUrl);
        return  codeUrl;
    }

    @Override
    public String orderquery(String orers) throws Exception {
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
        httpClient.setHttps(true);
        Map map = new HashMap();
        map.put("appid","wxab8acb865bb1637e");
        map.put("mch_id","11473623");
        map.put("nonce_str", WXPayUtil.generateNonceStr());
        map.put("out_trade_no",orers);// 订单号
        String xmlParam = WXPayUtil.generateSignedXml(map,"2ab9071b06b9f739b950ddb41db2690d");
        httpClient.setXmlParam(xmlParam);
        httpClient.post();
        String result = httpClient.getContent();
        System.out.println(result);
        Map<String,String> resultMap = WXPayUtil.xmlToMap(result);
        String trade_state = resultMap.get("trade_state");
        System.out.println("获取到的交易订单状态为："+trade_state);
        return trade_state;
    }
}
